package cn.com.jtang.wedding.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.jtang.wedding.model.Charge;
import cn.com.jtang.wedding.model.Orders;
import cn.com.jtang.wedding.model.User;
import cn.com.jtang.wedding.service.AdminUserManager;
import cn.com.jtang.wedding.service.ChargeManager;
import cn.com.jtang.wedding.service.OrdersManager;
import cn.com.jtang.wedding.service.UserManager;
import cn.com.jtang.wedding.util.EncryptUtil;
import cn.com.jtang.wedding.util.NumberUtil;

public class OrdersAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private OrdersManager ordersManager;
	private UserManager userManager;
	private ChargeManager chargeManager;
	private AdminUserManager adminUserManager;

	private Orders orders;
	private List<Orders> ordersList;
	private User user;
	private String effectTime;
	private String password1;//用于验证密码一致性



	private Long id;

	public String toAddOrders() {
		return SUCCESS;
	}

	//添加新订单
	public String addOrders() {
		if(userManager.queryByUsername(user.getUserName())!=null){
			return "existUser";
		}
		if(user.getPassword()==null||user.getPassword().trim().equals("")||!(user.getPassword().equals(password1))){
			return "password";
		}
		
		if (!adminUserManager.decreaseTimes(getCurrentAdminUser())) {
			return "emptyTimes";
		}


		//添加新用户
		user.setPassword(EncryptUtil.Encrypt(user.getPassword()));
		user.setRegistTime(new Date());
		user.setParentId(getCurrentAdminUser().getId());
		userManager.add(user);
		user = userManager.queryByUsername(user.getUserName());

		//添加新订单
		orders.setOrderNum(NumberUtil.getUniqueID());
		orders.setUserId(user.getId());
		orders.setAdminUserId(getCurrentAdminUser().getId());
		orders.setAdminUserName(getCurrentAdminUser().getAdminUserName());
		orders.setOrderTime(new Date());
		orders.setEffectTime(getFormatEffectTime());
		if ("0".equals(ordersManager.add(orders).trim())) {
			return SUCCESS;
		} else {
			adminUserManager.increaseTimes(getCurrentAdminUser());
			return "fail";
		}

	}
	
	//管理员用户添加新订单
	public String addOrdersAdmin() {
		if(user.getPassword()==null||user.getPassword().trim().equals("")||!(user.getPassword().equals(password1))){
			return "fail";
		}
		//添加新用户
		user.setPassword(EncryptUtil.Encrypt(user.getPassword()));
		user.setRegistTime(new Date());
		user.setParentId(getCurrentAdminUser().getId());
		userManager.add(user);
		user = userManager.queryByUsername(user.getUserName());

		//添加新订单
		orders.setOrderNum(NumberUtil.getUniqueID());
		orders.setUserId(user.getId());
		orders.setAdminUserId(getCurrentAdminUser().getId());
		orders.setAdminUserName(getCurrentAdminUser().getAdminUserName());
		orders.setOrderTime(new Date());
		orders.setEffectTime(getFormatEffectTime());	
		if ("0".equals(ordersManager.add(orders).trim())) {
			return SUCCESS;
		} else{
			return ERROR;
		}

	}

	private Date getFormatEffectTime() {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date=null;
		try {
			date=sdf.parse(effectTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	//列出自己售出的订单列表
	public String listOrders() {
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("adminUserId", getCurrentAdminUser().getId());

		int count = ordersManager.getCount(map);
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount=pageCount==0?1:pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		ordersList = ordersManager.queryAll(map, pageSize * (pageNum - 1), pageSize * pageNum);
		return SUCCESS;
	}

	//修改某一订单
	public String toEditOrders() {
		if (id == null||id.equals(Long.valueOf(0))) {
			return "fail";
		}
		orders = ordersManager.queryById(id);
		user = userManager.queryById(orders.getUserId());
		return SUCCESS;
	}
	
	public String editOrders(){
		User u=userManager.queryByUsername(user.getUserName());
		if(u!=null&&(!u.getId().equals(user.getId()))){
			return "existUser";
		}
		User orignalUser=userManager.queryById(user.getId());
		user.setParentId(orignalUser.getParentId());
		user.setPassword(orignalUser.getPassword());
		user.setRegistTime(orignalUser.getRegistTime());
		userManager.updateById(user.getId(), user);
		
		Orders orignalOrders=ordersManager.queryById(orders.getId());
		orders.setAdminUserId(orignalOrders.getAdminUserId());
		orders.setAdminUserName(orignalOrders.getAdminUserName());
		orders.setAlipayId(orignalOrders.getAlipayId());
		orders.setEffectTime(getFormatEffectTime());
		orders.setOrderNum(orignalOrders.getOrderNum());
		orders.setOrderTime(orignalOrders.getOrderTime());
		orders.setUserId(user.getId());
		ordersManager.update(orders);
		return SUCCESS;
	}
	
	/**
	 * 删除某定单
	 */
	public String deleteOrders(){
		if(id==null||id.equals(Long.valueOf(0))){
			return "fail";
		}
		orders=ordersManager.queryById(id);
		userManager.deleteById(orders.getUserId());
		ordersManager.delete(id);
		return SUCCESS;
	}
	
	/**
	 * 方法描述：当前用户订单列表
	 * @return
	 */
	public String orderList(){
		user = this.getCurrentUser();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userId", user.getId());
		int count = ordersManager.getCount(param);
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		ordersList = ordersManager.queryAll(param, pageSize * (pageNum - 1), pageSize * pageNum);
		return SUCCESS;
	}
	
	/**
	 * 方法描述：订单信息
	 * @return
	 */
	public String orderInfo(){
		// 
		orders = ordersManager.queryById(id);
		return SUCCESS;
	}
	
	public OrdersManager getOrdersManager() {
		return ordersManager;
	}

	public void setOrdersManager(OrdersManager ordersManager) {
		this.ordersManager = ordersManager;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public ChargeManager getChargeManager() {
		return chargeManager;
	}

	public void setChargeManager(ChargeManager chargeManager) {
		this.chargeManager = chargeManager;
	}

	public Orders getOrders() {
		return orders;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	public List<Orders> getOrdersList() {
		return ordersList;
	}

	public void setOrdersList(List<Orders> ordersList) {
		this.ordersList = ordersList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}



	public String getEffectTime() {
		return effectTime;
	}

	public void setEffectTime(String effectTime) {
		this.effectTime = effectTime;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public AdminUserManager getAdminUserManager() {
		return adminUserManager;
	}

	public void setAdminUserManager(AdminUserManager adminUserManager) {
		this.adminUserManager = adminUserManager;
	}






	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getChargeType() {
		if (orders == null) {
			return null;
		}
		return chargeManager.queryById(orders.getChargeId()).getChargeName();
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}
	
	public List<Charge> getChargeList(){
		int end=chargeManager.getCount(null);
		return chargeManager.queryAll(null, 0, end, " chargeName asc");
	}
}
